package com.ztman.rxjava.mvp.presenter;

import android.os.Bundle;

import com.ztman.rxjava.BaseActivity;
import com.ztman.rxjava.RxBusCommander;
import com.ztman.rxjava.model.baseEntity.BaseRxBusEvent;
import com.ztman.rxjava.mvp.view.BaseViewActivityIml;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by YJH on 2017/11/30.
 * 功能：针对 Activity
 * 抽象出来的视图层，逻辑功能处理的代理
 */

public abstract class ActivityPresenter<T extends BaseViewActivityIml> extends BaseActivity {

    /**
     * 使用这个对象，调用已经实现的对应activity，她的视图代理类的内容
     * 比如：atyPresenter 就可以用来调用 类BaseMainActivity中的内容< atyPresenter ==  MainActivityProxy对象 >
     */
    protected T atyPresenter;

    /**
     * 功能：这个是关键，这个方法会在终端activity类中实现；从而激活所有的方法
     * @return
     */
    protected abstract Class<T> getPresenterClass();
    public ActivityPresenter(){
        try {
            atyPresenter = getPresenterClass().newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("create BaseViewActivityIml error in ActivityPresenter.");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create BaseViewActivityIml error in ActivityPresenter.");
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        atyPresenter.createInIBaseView(getLayoutInflater(), null, savedInstanceState);
        setContentView(atyPresenter.getRootViewInIBaseView());

        initRxBusMethod();
        atyPresenter.InitWidgetInIBaseView();

        //先初始化 控件view ；在进行数据的初始化
        initView();
        initData();

    }

    protected abstract void initData();//初始化数据
    protected abstract void initView();//初始化控件对象
    protected abstract void receiveRxBuxCommanders(BaseRxBusEvent baseRxBusEvent);//接收RxBus 指令

    /**
     * 功能：初始化Rxbus 订阅
     * 在基类BaseActivity里，注册RxBus监听
     */
    private void initRxBusMethod() {
        mSubscription = RxBusCommander.getInstance().receiveCommander(BaseRxBusEvent.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseRxBusEvent>() {
                    @Override
                    public void call(BaseRxBusEvent baseRxBusEvent) {
                        //正常状态下

                        receiveRxBuxCommanders(baseRxBusEvent);
                        //比如，这个时候，从某个页面发来指令，让改变一下某个TextView显示的内容变化
//                        BaseRxBusEvent rxCommander = new BaseRxBusEvent();
//                        rxCommander.data = "aty-SelfInfoActivity";
//                        rxCommander.type = 1;
//                        RxBusCommander.getInstance().postCommander(rxCommander);

                        //这个是接受方法
//                        if(baseRxBusEvent.data.equals("aty-SelfInfoActivity") && 1 == baseRxBusEvent.type){
//                            TextView.setText("就是这么设置");
//
//                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //异常状况捕捉
                    }
                });
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (atyPresenter == null) {
            try {
                atyPresenter = getPresenterClass().newInstance();
            } catch (InstantiationException e) {
                throw new RuntimeException("create BaseViewActivityIml error in ActivityPresenter.");
            } catch (IllegalAccessException e) {
                throw new RuntimeException("create BaseViewActivityIml error in ActivityPresenter.");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //取消注册，以避免内存泄露
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }

        atyPresenter = null;
    }

}
